﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows;
using Red.Controls.Shell;
using Red.Controls.ViewModels;

namespace Red.Toolkit.TexArrayEditor
{
    /// <summary>
    ///     This is how we make the editor a plug-in
    /// </summary>
    [Export( typeof( IEditorInfo ) )]
    public class EditorInfo : IEditorInfo
    {
        public string Name
        {
            get { return "Tex array editor"; }
        }

        public Type WindowClass
        {
            get { return typeof( TexArrayEditor ); }
        }

        public Type SettingsClass
        {
            get { return typeof( TexArrayEditorSettings ); }
        }
    }
    
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class TexArrayEditor : RedEditorWindow
    {
        public TexArrayEditor(IEditorHost host, EditorInstanceData instanceData, EditorSharedData sharedData)
            : base( host, instanceData, sharedData )
        {
            InitializeComponent();
        }

        private void AddBtn_Click(object sender, RoutedEventArgs e)
        {
            string sourceExtensions = "CBitmapTexture (*.xbm)|*.xbm";
            var dialog = new Microsoft.Win32.OpenFileDialog
            {
                InitialDirectory = SharedData.DepotInfo.RootPath,
                Multiselect = true,
                Filter = sourceExtensions,
                Title = @"Select source file"
            };

            if (true.Equals(dialog.ShowDialog()))
            {
                List<string> fileDepotPaths = new List<string>();
                for (int i = 0; i < dialog.FileNames.Count(); ++i)
                {
                    fileDepotPaths.Add(SharedData.DepotInfo.GetDepotPath(dialog.FileNames[i]));
                }

                InteropDataItem item = DataContext as InteropDataItem;
                if (item != null)
                {
                    for (int i = 0; i < fileDepotPaths.Count(); ++i)
                    {
                        item.Execute("addbitmap", fileDepotPaths[i]);
                        item.Execute("compile");
                    }
                }
            }
        }
    }
}
